package io.ai.arch.java.oss.aliyun;


import com.aliyun.oss.*;
        import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketStat;

import java.util.Date;
import java.util.List;

public class AliyunOSS {
    public static void main(String[] args) throws Exception {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        DefaultCredentialProvider credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider("", "");
        // 填写Bucket名称，例如examplebucket。;
        // 填写Bucket所在地域。以华东1（杭州）为例，Region填写为cn-hangzhou。
        String region = "cn-shanghai";

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // 列举当前账号所有地域下的存储空间。
            List<Bucket> buckets = ossClient.listBuckets();
            for (Bucket bucket : buckets) {
                if ("x".equalsIgnoreCase(bucket.getName())
                ) {
                    System.out.println(" - " + bucket.getName());
                    try {
            //            StorageSize:195085666673
            //            ObjectCount:1923190
            //            MultipartUploadCount:1
            //            StandardStorage:195085666673
//            StandardObjectCount:1923190
                        BucketStat stat = ossClient.getBucketStat(bucket.getName());
                        // 获取Bucket的总存储量，单位为字节。
                        System.out.println("StorageSize:" + stat.getStorageSize() / 1024 / 1024 / 1024 + "GB");
                        // 获取Bucket中总的Object数量。
                        System.out.println("ObjectCount:" + stat.getObjectCount());
                        // 获取Bucket中已经初始化但还未完成（Complete）或者还未中止（Abort）的Multipart Upload数量。
//                        System.out.println("MultipartUploadCount:" + stat.getMultipartUploadCount());
                        // 获取标准存储类型Object的存储量，单位为字节。
                        System.out.println("StandardStorage:" + stat.getStandardStorage() / 1024 / 1024 / 1024 + "GB");
                        // 获取标准存储类型的Object的数量。
                        System.out.println("StandardObjectCount:" + stat.getStandardObjectCount());
//                    // 获取Bucket中Live Channel的数量。
//                    System.out.println("LiveChannelCount:"+stat.getLiveChannelCount());
//                     此次调用获取到的存储信息的时间点，格式为时间戳，单位为秒。
                    System.out.println("LastModifiedTime:"+stat.getLastModifiedTime() + new Date(stat.getLastModifiedTime() * 1000));
//                    // 获取低频存储类型Object的计费存储量，单位为字节。
//                    System.out.println("InfrequentAccessStorage:"+stat.getInfrequentAccessStorage());
//                    // 获取低频存储类型Object的实际存储量，单位为字节。
//                    System.out.println("InfrequentAccessRealStorage:"+stat.getInfrequentAccessRealStorage());
//                    // 获取低频存储类型的Object数量。
//                    System.out.println("InfrequentAccessObjectCount:"+stat.getInfrequentAccessObjectCount());
//                    // 获取归档存储类型Object的计费存储量，单位为字节。
//                    System.out.println("ArchiveStorage:"+stat.getArchiveStorage());
//                    // 获取归档存储类型Object的实际存储量，单位为字节。
//                    System.out.println("ArchiveRealStorage:"+stat.getArchiveRealStorage());
//                    // 获取归档存储类型的Object数量。
//                    System.out.println("ArchiveObjectCount:"+stat.getArchiveObjectCount());
//                    // 获取冷归档存储类型Object的计费存储量，单位为字节。
//                    System.out.println("ColdArchiveStorage:"+stat.getColdArchiveStorage());
//                    // 获取冷归档存储类型Object的实际存储量，单位为字节。
//                    System.out.println("ColdArchiveRealStorage:"+stat.getColdArchiveRealStorage());
//                    // 获取冷归档存储类型的Object数量。
//                    System.out.println("ColdArchiveObjectCount:"+stat.getColdArchiveObjectCount());
                    } catch (OSSException oe) {
                        System.out.println("Caught an OSSException, which means your request made it to OSS, "
                                + "but was rejected with an error response for some reason.");
                        System.out.println("Error Message:" + oe.getErrorMessage());
                        System.out.println("Error Code:" + oe.getErrorCode());
                        System.out.println("Request ID:" + oe.getRequestId());
                        System.out.println("Host ID:" + oe.getHostId());
                    } catch (ClientException ce) {
                        System.out.println("Caught an ClientException, which means the client encountered "
                                + "a serious internal problem while trying to communicate with OSS, "
                                + "such as not being able to access the network.");
                        System.out.println("Error Message:" + ce.getMessage());
                    } finally {
//                    if (ossClient != null) {
//                        ossClient.shutdown();
//                    }
                    }
                }
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}